﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.ComponentModel;

namespace HospitalSystem
{
    public partial class register : System.Web.UI.Page
    {
       // SqlConnection conn = new SqlConnection(@"Data Source=SJ-PC\SQLEXPRESS;Initial Catalog=db0015;Integrated Security=True");
        string strConnString = ConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

            String ShowValue = (string)Session["CID"];
            Label4.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label3.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label2.Text = ShowValue3;
            show_Image();

            if (!IsPostBack)
            {
                loaddataall();
             
            }
           


        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

       // protected void Button1_Click(object sender, EventArgs e)
       // {
           

         //   string date2 = TextBox1.Text;
       //     DateTime convertdate = DateTime.Parse(date2);
           
//
         //   SqlCommand CmdSql = new SqlCommand("INSERT INTO [date] (date) VALUES (@name)", conn);
           // conn2.Open();
          //  CmdSql.Parameters.AddWithValue("@name", convertdate);


        //    CmdSql.ExecuteNonQuery();
          //  conn2.Close();



       //     MessageBox("ลงทะเบียนเรียบร้อยครับ");

       // }

      
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
         
           
        }



        protected void Button1_Click1(object sender, EventArgs e)
        {

            //string date2 DateTime.Now();
         //   string date2 = TextBox4.Text;
           //DateTime convertdatedob = DateTime.Parse(date2);
            string date2 = TextBox4.Text;
            DateTime convertdatedob = DateTime.Parse(date2);
            string date3 = TextBox42.Text;
            DateTime LiveMoveInDate = DateTime.Parse(date3);
              string date4 = TextBox43.Text;
              DateTime LiveMoveOutDate = DateTime.Parse(date4);
            
                 string date5 = TextBox44.Text;
                 DateTime CensusMoveInDate = DateTime.Parse(date5);


              string date6 = TextBox45.Text;
              DateTime CensusMoveOutDate = DateTime.Parse(date6);

              string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
              SqlConnection conn2 = new SqlConnection(constr);

              SqlCommand CmdSql = new SqlCommand("UPDATE [Person] SET Gender=@Gender,BloodType=@BloodType,FirstName=@FirstName,LastName=@LastName,DOB=@DOB,Nation=@Nation,Race=@Race,Religion=@Religion,Foreigner=@Foreigner,LiveHomeStatus=@LiveHomeStatus,EducationStatus=@EducationStatus,LiveStreetAddress=@LiveStreetAddress,LiveStreetName=@LiveStreetName,Livetumbon=@Livetumbon,LiveCity=@LiveCity,LiveProvince=@LiveProvince,LivePostcode=@LivePostcode,CensusStreetAddress=@CensusStreetAddress,CensusStreetName=@CensusStreetName,Censustumbon=@Censustumbon,CensusCity=@CensusCity,CensusProvince=@CensusProvince,CensusPostcode=@CensusPostcode,HomePhone=@HomePhone,Mobile=@Mobile,DateTimeUpdate=@DateTimeUpdate,LiveMoveInDate=@LiveMoveInDate,LiveMoveOutDate=@LiveMoveOutDate,CensusMoveInDate=@CensusMoveInDate,CensusMoveOutDate=@CensusMoveOutDate,LiveMoveOutReason=@LiveMoveOutReason,CensusMoveOutReason=@CensusMoveOutReason WHERE CID=@CID", conn2);
            //

              //  ,[Gender]
              //    ,[BloodType]



            conn2.Open();


            CmdSql.Parameters.AddWithValue("@Gender", RadioButtonList1.SelectedValue);
            CmdSql.Parameters.AddWithValue("@BloodType", RadioButtonList2.SelectedValue);
            CmdSql.Parameters.AddWithValue("@FirstName", TextBox1.Text);
            CmdSql.Parameters.AddWithValue("@LastName", TextBox2.Text);
            CmdSql.Parameters.AddWithValue("@DOB", convertdatedob);
            CmdSql.Parameters.AddWithValue("@CID", Label4.Text);
            CmdSql.Parameters.AddWithValue("@Nation", TextBox6.Text);
            CmdSql.Parameters.AddWithValue("@Race", TextBox5.Text);
            CmdSql.Parameters.AddWithValue("@Religion", TextBox8.Text);
            CmdSql.Parameters.AddWithValue("@Foreigner", TextBox19.Text);
            CmdSql.Parameters.AddWithValue("@LiveHomeStatus", DropDownList7.Text);
            CmdSql.Parameters.AddWithValue("@EducationStatus", DropDownList18.Text);

            //LiveStreetAddress=@LiveStreetAddress,LiveStreetName=@LiveStreetName,Livetumbon=@Livetumbon,LiveCity=@LiveCity,LiveProvince=@LiveProvince,LivePostcode=@LivePostcode,CensusStreetAddress=@CensusStreetAddress,CensusStreetName=@CensusStreetName,Censustumbon=@Censustumbon,CensusCity@CensusCity,CensusProvince=@CensusProvince,CensusPostcode=@CensusPostcode,HomePhone@HomePhone,Mobile=@Mobile,DateTimeUpdate=@DateTimeUpdate
            // LiveMoveInDate
            
            CmdSql.Parameters.AddWithValue("@LiveStreetAddress", TextBox32.Text);
            CmdSql.Parameters.AddWithValue("@LiveStreetName", TextBox33.Text);
            CmdSql.Parameters.AddWithValue("@Livetumbon", TextBox36.Text);
            CmdSql.Parameters.AddWithValue("@LiveCity", TextBox37.Text);
            CmdSql.Parameters.AddWithValue("@LiveProvince", TextBox38.Text);
            CmdSql.Parameters.AddWithValue("@LivePostcode", TextBox7.Text);
            CmdSql.Parameters.AddWithValue("@CensusStreetAddress", TextBox34.Text);
            CmdSql.Parameters.AddWithValue("@CensusStreetName", TextBox35.Text);
            CmdSql.Parameters.AddWithValue("@Censustumbon", TextBox39.Text);
            CmdSql.Parameters.AddWithValue("@CensusCity", TextBox40.Text);
            CmdSql.Parameters.AddWithValue("@CensusProvince", TextBox41.Text);
            CmdSql.Parameters.AddWithValue("@CensusPostcode", TextBox9.Text);
            CmdSql.Parameters.AddWithValue("@HomePhone", TextBox10.Text);
            CmdSql.Parameters.AddWithValue("@Mobile", TextBox11.Text);
            CmdSql.Parameters.AddWithValue("@RecordedStaffID", Label1.Text);

            CmdSql.Parameters.AddWithValue("@DateTimeUpdate", DateTime.Now);
            CmdSql.Parameters.AddWithValue("@LiveMoveInDate", LiveMoveInDate);
            //CensusMoveInDate       CensusMoveOutDate LiveMoveOutReason  CensusMoveOutReason
            CmdSql.Parameters.AddWithValue("@LiveMoveOutReason", RadioButtonList3.SelectedValue);
            CmdSql.Parameters.AddWithValue("@CensusMoveOutReason", RadioButtonList4.SelectedValue);

            CmdSql.Parameters.AddWithValue("@LiveMoveOutDate", LiveMoveOutDate);
            CmdSql.Parameters.AddWithValue("@CensusMoveOutDate", CensusMoveOutDate);
            CmdSql.Parameters.AddWithValue("@CensusMoveInDate", CensusMoveInDate);
            CmdSql.ExecuteNonQuery();
            conn2.Close();


      



            MessageBox("ลงทะเบียนเรียบร้อยครับ");

            Session["CID"] = TextBox3.Text;




            ImageUpload_cid();
            Response.Redirect("~/regfamily.aspx");







          
        }

        protected void Button2_Click(object sender, EventArgs e)
        {



         //   Session["LastName"] = TextBox2.Text;
         //   Session["FirstName"] = TextBox1.Text;
           // Session["CID"] = TextBox3.Text;
            Response.Redirect("~/regfamily.aspx");



        }

        protected void loaddataall()
        {





            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);


            SqlCommand CmdSql2 = new SqlCommand("SELECT Gender,BloodType,CID,Title,FirstName,LastName,DOB,Nation,Race,Religion,Foreigner,LiveHomeStatus,EducationStatus,LiveStreetAddress,LiveStreetName,Livetumbon,LiveCity,LiveProvince,LivePostcode,CensusStreetAddress,CensusStreetName,Censustumbon,CensusCity,CensusProvince,CensusPostcode,HomePhone,Mobile,RecordedStaffID,LiveMoveInDate,LiveMoveOutDate,CensusMoveInDate,CensusMoveOutDate,LiveMoveOutReason,CensusMoveOutReason FROM [Person] where CID=@CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Label4.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

            //CmdSql.Parameters.AddWithValue("@Gender", RadioButtonList1.SelectedValue);
            // CmdSql.Parameters.AddWithValue("@BloodType", RadioButtonList2.SelectedValue);
            // CensusCity,CensusProvince,CensusPostcode,HomePhone,Mobile,RecordedStaffID
            if (reader2.Read())
            {
                RadioButtonList1.SelectedValue = reader2["Gender"].ToString();
                RadioButtonList2.SelectedValue = reader2["BloodType"].ToString();
                TextBox1.Text = reader2["FirstName"].ToString();
                TextBox2.Text = reader2["LastName"].ToString();
                TextBox3.Text = reader2["CID"].ToString();
                TextBox4.Text = reader2["DOB"].ToString();
                TextBox6.Text = reader2["Nation"].ToString();
                TextBox5.Text = reader2["Race"].ToString();
                TextBox8.Text = reader2["Religion"].ToString();
                TextBox19.Text = reader2["Foreigner"].ToString();
                TextBox32.Text = reader2["LiveStreetAddress"].ToString();
                TextBox33.Text = reader2["LiveStreetName"].ToString();
                TextBox36.Text = reader2["Livetumbon"].ToString();
                TextBox37.Text = reader2["LiveCity"].ToString();
                TextBox38.Text = reader2["LiveProvince"].ToString();
                TextBox7.Text = reader2["LivePostcode"].ToString();
                TextBox34.Text = reader2["CensusStreetAddress"].ToString();
                TextBox35.Text = reader2["CensusStreetName"].ToString();
                TextBox39.Text = reader2["Censustumbon"].ToString();
                TextBox40.Text = reader2["CensusCity"].ToString();
                TextBox41.Text = reader2["CensusProvince"].ToString();
                TextBox9.Text = reader2["CensusPostcode"].ToString();
                TextBox10.Text = reader2["HomePhone"].ToString();
                TextBox11.Text = reader2["Mobile"].ToString();
                Label1.Text = reader2["RecordedStaffID"].ToString();

                //LiveMoveInDate,LiveMoveOutDate,CensusMoveInDate,CensusMoveOutDate  LiveMoveOutReason,CensusMoveOutReason
                RadioButtonList3.SelectedValue = reader2["LiveMoveOutReason"].ToString();
                RadioButtonList4.SelectedValue = reader2["CensusMoveOutReason"].ToString();
                TextBox42.Text = reader2["LiveMoveInDate"].ToString();
                TextBox43.Text = reader2["LiveMoveOutDate"].ToString();
                TextBox44.Text = reader2["CensusMoveInDate"].ToString();
                TextBox45.Text = reader2["CensusMoveOutDate"].ToString();


                DropDownList18.Text = reader2["EducationStatus"].ToString();
                DropDownList7.Text = reader2["LiveHomeStatus"].ToString();
                DropDownList1.Text = reader2["Title"].ToString();



                //   RadioButtonList1.Text = reader2["FMstatust"].ToString();
                //     RadioButton1.Text = reader2["FMstatust"].ToString();
            }
            else
            {
                //MessageBox("ไม่พบเลขบัตรครับ");
            }
            conn.Close();
            reader2.Close();








        }





        protected void ImageUpload_f()
        {
            SqlConnection conn = new SqlConnection(strConnString);
            if (FileUploader.HasFile)
            {
                //try
                //{
                


                    conn.Open();
                    SqlCommand cmd = new SqlCommand("delete from ImageDek where Iddek=@CID", conn);
                    cmd.Parameters.AddWithValue("@CID", Label4.Text);
                    cmd.ExecuteNonQuery();

                    conn.Close();
          




                    //ไปใส่พาธไฟล์เอา

                    string MapPath = ("~/pic/profile/");
                    string filename = Label4.Text;
                    FileUploader.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUploader.FileName));
                    //FileUploader.SaveAs(filename + ".Jpeg");
                    // string SQL = "INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)";
                    //เพิ่มไฟล์ไทป์ด้วยนะคะพี่รงคะ
                    string Includepath = ((MapPath) + filename + Path.GetExtension(FileUploader.FileName));
                    Bitmap originalBMP = new Bitmap(FileUploader.FileContent);
                    // ใส่ขนาดที่นี่
                    int newWidth = 200;
                    int newHeight = 200;
                    Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                    Graphics oGraphics = Graphics.FromImage(newBMP);
                    oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                    newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUploader.FileName)));
                    originalBMP.Dispose();
                    newBMP.Dispose();
                    oGraphics.Dispose();

                    string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                    SqlConnection scon = new SqlConnection(constr);
                    SqlCommand cmd22 = new SqlCommand("INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)", scon);
                    cmd22.Parameters.AddWithValue("@Iddek", Label4.Text);
                    cmd22.Parameters.AddWithValue("@FilePath", Includepath);



                    scon.Open();
                    cmd22.ExecuteNonQuery();
                    scon.Close();

                    Response.Redirect("~/register.aspx");
                    //xxx คิวรีมาจาก db เอา
                    //Image1.ImageUrl = Server.MapPath("xxx");
            



                //}
                //catch
                //{

                //}
            }
        }

        protected void ImageUpload_Click(object sender, EventArgs e)
        {
            ImageUpload_f();
        }




        protected void ImageUpload_cid()
        {
            SqlConnection conn = new SqlConnection(strConnString);
            if (FileUploadercard.HasFile)
            {
                //try
                //{
                if (FileUploadercard.PostedFile.ContentType == "image/jpeg")
                {


                    conn.Open();
                    SqlCommand cmd = new SqlCommand("delete from pic_cid where cid=@CID", conn);
                    cmd.Parameters.AddWithValue("@CID", Label4.Text);
                    cmd.ExecuteNonQuery();

                    conn.Close();





                    //ไปใส่พาธไฟล์เอา

                    string MapPath = ("~/pic/cid/");
                    string filename = Label4.Text;
                    FileUploadercard.SaveAs(Server.MapPath(MapPath) + filename + ".Jpeg");
                    //FileUploader.SaveAs(filename + ".Jpeg");
                    // string SQL = "INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)";
                    //เพิ่มไฟล์ไทป์ด้วยนะคะพี่รงคะ
                    string Includepath2 = ((MapPath) + filename + ".jpg");
                    string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                    SqlConnection scon = new SqlConnection(constr);
                    SqlCommand cmd22 = new SqlCommand("INSERT INTO pic_cid (cid,[FilePath]) VALUES (@Iddek,@Filepath)", scon);
                    cmd22.Parameters.AddWithValue("@Iddek", Label4.Text);
                    cmd22.Parameters.AddWithValue("@FilePath", Includepath2);



                    scon.Open();
                    cmd22.ExecuteNonQuery();
                    scon.Close();

                    //xxx คิวรีมาจาก db เอา
                    //Image1.ImageUrl = Server.MapPath("xxx");
                }
                else
                {
                }



                //}
                //catch
                //{

                //}
            }
        }











        protected void show_Image()
        { 
         string sj = "";
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);
       
            
            SqlCommand CmdSql2 = new SqlCommand("SELECT FilePath FROM [ImageDek] where Iddek=@CID", conn);
            conn.Open();
          CmdSql2.Parameters.AddWithValue("@CID", Label4.Text);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();

              if (reader2.Read())
            {
                sj = reader2["FilePath"].ToString();
            }
              else
              {
                  sj = "~/pic/profile/nopic.jpg";
              }
              conn.Close();
              reader2.Close();

          
            Image1.ImageUrl = (sj);
        }
        }
        }
 